<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 Github Copilot 插件</title>
    <style>
        body, html {
            height: 100%;
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
        }

        .login-container {
            background-color: white;
            padding: 6em;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            text-align: center;
            box-sizing: border-box;
            width: 50vw; /* 添加固定宽度 */
            min-width: 450px; /* 添加最小宽度 */
            max-width: 650px;
        }

        input {
            width: 100%;
            padding: 1em;
            margin: 0.5em 0;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-sizing: border-box; /* 确保填充和边框包含在宽度和高度中 */
            font-size: 16px;
        }

        button {
            width: 100%;
            padding: .8em;
            margin-top: 1em;
            border: none;
            border-radius: 5px;
            background-color: #007bff;
            color: white;
            font-size: 1em;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        /* Dark mode styles */
        @media (prefers-color-scheme: dark) {
            body, html {
                background-color: #121212;
                color: white;
            }

            .login-container {
                background-color: #1e1e1e;
                box-shadow: 0 4px 8px rgba(0, 0, 0, 0.5);
            }

            input {
                background-color: #2e2e2e;
                border: 1px solid #444;
                color: white;
            }

            input::placeholder {
                color: #888;
            }

            button {
                background-color: #007bff;
                color: white;
            }

            button:hover {
                background-color: #0056b3;
            }
        }

        .footer {
            margin-top: 20px;
            font-size: 12px;
            color: #aaa;
        }

        .footer a {
            color: #aaa;
        }

        #password {
            display: none;
        }
    </style>
</head>
<body>
<div class="login-container">
    <h1>登录 Github Copilot 插件</h1>
    <form onsubmit="submitForm()">
        <input type="text" id="password" class="input-field"
               placeholder="请输入访问密码, 在环境变量LOGIN_PASSWORD参数中">
        <input type="text" id="authorization" class="input-field"
               placeholder="请输入剪贴板内的授权码 (Ctrl+V直接粘贴即可)">
        <button type="submit">登 录</button>
    </form>

    <div class="footer">
        <a href="https://gitee.com/ripperTs/github-copilot-proxies" target="_blank">Copilot后端代理服务</a>
    </div>
</div>
<script>

    async function getLoginConfig() {
        const result = await fetch('/login/config');
        const resultJson = await result.json();
        if (resultJson.is_login_password) {
            document.getElementById('password').style.display = 'block';
        }
    }

    getLoginConfig();

    function getQueryParam(param) {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get(param);
    }

    function setCodeInputValue(){
        const code = getQueryParam('user_code');
        if (code !== null) {
            document.getElementById('authorization').value = code;
        }
    }
    setCodeInputValue();

    async function submitForm() {
        event.preventDefault();
        const authorization = document.getElementById('authorization').value;
        const password = document.getElementById('password').value;
        const code = getQueryParam('user_code');

        if (code === null) {
            alert('链接打开方式不正确，请重新打开');
            return false;
        }

        if (authorization === '') {
            alert('请输入授权码');
            return false;
        }

        try {
            const response = await fetch('/login/device', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({code, authorization, password})
            })

            if (!response.ok) {
                alert('HTTP error! status: ' + response.status);
                return false;
            }

            const result = await response.json();
            if (result?.error !== 0) {
                alert(result.message);
                return false;
            }
            alert('登录成功, 请返回IDE查看并使用');
            window.close();
        } catch (e) {
            alert('提交表单时出错，请稍后再试');
        }
    }

</script>
</body>
</html>
